package com.baidu.sms.controller;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class SmsListener {
    @JmsListener(destination = "springboot-sms")

    public void list1(Map map){
        String phone = (String) map.get("phone");
        String smsCode = (String) map.get("smsCode");
        DefaultProfile profile = DefaultProfile.getProfile(
                "cn-hangzhou",  //地域ID
                "LTAIIQJgD2IIsDLL",//RAM账号的AccessKey ID
                "Xualwj4RAGen5bU4tcnO03L8Rz5V3f");//RAM账号Access Ley Secret
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        /*SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(new Date());*/
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "八维云计算");
        request.putQueryParameter("TemplateCode", "SMS_143717641");
        request.putQueryParameter("TemplateParam", "{\"code\":\""+smsCode+"\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
